﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Manage.Controller;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// ChanceMouldAjax 的摘要说明
    /// </summary>
    public class ChanceMouldAjax : IHttpHandler
    {

        ChanceMouldController controller = new ChanceMouldController();
        ChanceController chanceController = new ChanceController();
        ChanceTypeController typeController = new ChanceTypeController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetMouldList":
                        GetMouldList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "GetTaskCondition"://获取可作为任务条件的模板
                        GetTaskCondition(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetTaskCondition(HttpContext context)
        {
            List<ChanceMould> list = controller.GetList(" IsTaskCondition=1");
            string json = JsonHelper.ListToJson<ChanceMould>(list);
            context.Response.Write(json);
        }

        #region chanceMould
        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            bool isSuccess = controller.Delete(id);
            if (isSuccess)
            {
                chanceController.DeleteByName(id);
            }
            context.Response.Write(isSuccess);
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            string typeName = context.Request["typeName"];
            string name = context.Request["name"];
            int sortNum = Convert.ToInt32(context.Request["sortNum"].ToString());
            bool isTaskCondition = Convert.ToBoolean(context.Request["isTaskCondition"].ToString());
            ChanceMould model = new ChanceMould()
            {
                ID = id,
                SortNum = sortNum,
                IsTaskCondition = isTaskCondition,
                MouldName = name,
                TypeName = typeName
            };
            bool isSuccess = controller.Update(model);

            context.Response.Write(isSuccess);
        }

        private void Add(HttpContext context)
        {
            bool isSuccess = false;
            string name = context.Request["name"];
            string typeName = context.Request["typeName"];
            int sortNum = Convert.ToInt32(context.Request["sortNum"].ToString());
            bool isTaskCondition = Convert.ToBoolean(context.Request["isTaskCondition"].ToString());
            int mouldID = 0;
            if (!string.IsNullOrEmpty(name.Trim()))
            {
                ChanceMould model = new ChanceMould()
                {
                    MouldName = name,
                    SortNum = sortNum,
                    TypeName = typeName,
                    IsTaskCondition = isTaskCondition
                };
                mouldID = controller.Add(model);

                if (mouldID != 0)
                {
                    List<ChanceType> typeList = typeController.GetList();
                    foreach (var item in typeList)
                    {
                        Chance chance = new Chance()
                        {
                            TypeID = item.ID,
                            ChanceMouldID = mouldID,
                            ChanceOperator = "=",
                            MaxNum = 0,
                            MinNum = 0,
                        };
                        isSuccess = chanceController.Add(chance);
                    }
                }
            }
            context.Response.Write(mouldID);
        }

        private void GetMouldList(HttpContext context)
        {
            List<ChanceMould> list = controller.GetList();
            string json = JsonHelper.ListToJson<ChanceMould>(list);
            context.Response.Write(json);
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}